package com.kee.server.manager.service;

import java.util.Date;

import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

import com.kee.server.manager.bean.Content;
import com.kee.server.manager.bean.ContentChannel;
import com.kee.server.manager.bean.ContentCheck;
import com.kee.server.manager.bean.ContentCount;
import com.kee.server.manager.bean.ContentExt;
import com.kee.server.manager.bean.ContentTxt;
import com.kee.server.manager.dao.ContentChannelImpl;
import com.kee.server.manager.dao.ContentCheckImpl;
import com.kee.server.manager.dao.ContentCountImpl;
import com.kee.server.manager.dao.ContentDaoImpl;
import com.kee.server.manager.dao.ContentExtDaoImpl;
import com.kee.server.manager.dao.ContentTxtDaoImpl;
import com.kee.server.manager.dao.IContentChannel;
import com.kee.server.manager.dao.IContentCheck;
import com.kee.server.manager.dao.IContentCount;
import com.kee.server.manager.dao.IContentDao;
import com.kee.server.manager.dao.IContentExtDao;
import com.kee.server.manager.dao.IContentTxtDao;
import com.kee.share.bean.Exam;

public class DataHandler {
	
	private final static Whitelist whitelist = new Whitelist();
	
	static {
		whitelist.addTags("img");
	}
	
	private final static IContentDao contentDao = new ContentDaoImpl();

	private final static IContentChannel contentChannelDao = new ContentChannelImpl();

	private final static IContentCheck contentCheckDao = new ContentCheckImpl();

	private final static IContentExtDao contentExtDao = new ContentExtDaoImpl();
	
	private final static IContentTxtDao contentTxtDao = new ContentTxtDaoImpl();
	
	private final static IContentCount contentCountDao = new ContentCountImpl();
	
	
	private PushPlugin pushPlugin;
	
	public DataHandler(PushPlugin pushPlugin) {
		this.pushPlugin = pushPlugin;
	}
	
	public void push(int channeld,int userId,int typeId,int modelId,Exam exam) throws Exception{
		//content表中插入数据
		Content c = new Content();
		c.setChannel_id(channeld);
		c.setUser_id(userId);
		c.setType_id(typeId);
		c.setModel_id(modelId);
		c.setSort_date(new Date());
		c.setTop_level(0);
		c.setHas_title_img(0);
		c.setComments_day(0);
		c.setDownloads_day(0);
		c.setIs_recommend(0);
		c.setViews_day(0);
		c.setUps_day(0);
		c.setStatus(2);
		c.setSite_id(1);
		int content_id = contentDao.add(c);
		
		//内容渠道表中插入数据
		ContentChannel contentChannel = new ContentChannel();
		contentChannel.setChannel_id(channeld);
		contentChannel.setContent_id(content_id);
		contentChannelDao.add(contentChannel);
		//内容审核表中插入数据
		ContentCheck check = new ContentCheck();
		check.setContent_id(content_id);
		check.setCheck_date(new Date());
		check.setCheck_step(2);
		check.setIs_rejected(0);
		contentCheckDao.add(check);
		//内容扩张表
		ContentExt ext = new ContentExt();
		ext.setContent_id(content_id);
		System.out.println(exam);
		String title = Jsoup.clean(exam.getExam(), whitelist);
		title = StringEscapeUtils.unescapeHtml4(title);
		ext.setTitle(title.length()>70?title.substring(0, 70):title);
		//ext.setDescription("ttt");
		ext.setRelease_date(new Date());
		ext.setNeed_regenerate(1);
		contentExtDao.add(ext);
		//内容文本表
		ContentTxt txt = new ContentTxt();
		txt.setContent_id(content_id);
		txt.setTxt(pushPlugin.getTitle());
		//String answer = Jsoup.clean(exam.getAnswer(), whitelist);
		txt.setTxt1(pushPlugin.getAnalyze());
		txt.setTxt2(exam.getKnowledge());
		contentTxtDao.add(txt);
		// 内容统计表
		ContentCount count = new ContentCount();
		count.setContent_id(content_id);
		contentCountDao.add(count);
	}
	
}
